/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.reporte.control;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author 43579648
 */
public abstract class AbstractFacade<T> {
    private Class<T> entityClass;
    private T entity;

    public AbstractFacade(Class<T> entityClass) {
            this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();
    
    
    public void create() throws Exception {
        try{
            getEntityManager().getTransaction().begin();
            getEntityManager().persist(entity);
            getEntityManager().getTransaction().commit();
        }catch(Exception e){
            e.printStackTrace();
            getEntityManager().getTransaction().rollback();
            throw new Exception(e);
        }
    }

    
    public void edit() throws Exception {
        try{
            getEntityManager().getTransaction().begin();
            getEntityManager().merge(entity);
            getEntityManager().flush();
            getEntityManager().getTransaction().commit();
        }catch(Exception e){
            e.printStackTrace();
            getEntityManager().getTransaction().rollback();
            throw new Exception(e);
        }
    }

    public void remove() throws Exception {
        try{
            getEntityManager().getTransaction().begin();
            getEntityManager().remove(getEntityManager().merge(entity));
            getEntityManager().getTransaction().commit();
        }catch(Exception e){
            e.printStackTrace();
            getEntityManager().getTransaction().rollback();
            throw new Exception(e);
        }
    }

    public void create(T entity) throws Exception {
        try{
            getEntityManager().getTransaction().begin();
            getEntityManager().persist(entity);
            getEntityManager().getTransaction().commit();
        }catch(Exception e){
            e.printStackTrace();
            getEntityManager().getTransaction().rollback();
            throw new Exception(e);
        }
    }

    public void edit(T entity) throws Exception {
        try{
            getEntityManager().getTransaction().begin();
            getEntityManager().merge(entity);
            getEntityManager().getTransaction().commit();
            getEntityManager().flush();
        }catch(Exception e){
            e.printStackTrace();
            getEntityManager().getTransaction().rollback();
            throw new Exception(e);
        }
    }

    public void remove(T entity) throws Exception {
        try{
            getEntityManager().getTransaction().begin();
            getEntityManager().remove(getEntityManager().merge(entity));
            getEntityManager().getTransaction().commit();
        }catch(Exception e){
            e.printStackTrace();
            getEntityManager().getTransaction().rollback();
            throw new Exception(e);
        }
    }

    public T find(Object id) {
        
        return getEntityManager().find(entityClass, id);
    }

    public List<T> findAll() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        List<T> list = getEntityManager().createQuery(cq).getResultList();
        for(T t : list){
            getEntityManager().refresh(t);
        }
        return list;
    }
    
    
    public List<T> findAllDistinct() {
        Query q = getEntityManager().createQuery("Select distinct o from " + entityClass.getSimpleName() + " o");
        return q.getResultList();
    }

    public List<T> findRange(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }

    public T getEntity() {
        return entity;
    }

    public void setEntity(T entity) {
        this.entity = entity;
    }
    
    
}
